//ТЕРЕХОВ copyright 2011
int size = 5;       // Width of the shape
float xpos, ypos;    // Starting position of shape   
float xspeed = 3.8;  // Speed of the shape
float yspeed = 6.3;  // Speed of the shape
int xdirection = 1;  // Left or Right
int ydirection = 1;  // Top to Bottom

float pad_width=50;
float pad_height=5;
float floor_dist=15;

int [][] visible= new int[4][10];//видимость боксов
float [][] bonus= new float[4][10]; //бонус
int visible_bonus=1;
float speed_bonus_x, speed_bonus_y;
float bonus_speed = 2.8;
int speed_bonus_val;
int visible_count=0;
    

int life=3;



void setup()
{
    size(400, 400);
  noStroke();
  frameRate(30);
  smooth();
  // Set the starting position of the shape
  xpos = width/2;
  ypos = height/2;
  for(int j=0;j<4;j++)
  {
  for(int i=0;i<10;i++)
  {
   visible[j][i]=1; 
   bonus[j][i]=random(8);
  }
  }

}

void draw() 
{
  background(600);
  
  //nocursor
    noCursor();
 //pause
 if( ypos >height )
 {
  for(int i=1;i>0;i=i)
 {
   ypos= 200;
   xpos=200;
   
 } 
 }
 
 for(int j=0;j<4;j++)
  {
  for(int i=0;i<10;i++)
  {
     if(visible[j][i] == 0)
     {
      
      visible_count+=1;
     }
     if(visible_count==40)
     {
        javax.swing.JOptionPane.showMessageDialog(null, "Victory!");  
          exit();
     }
     
  }
    
  }
  
  // Update the position of the shape
  xpos = xpos + ( xspeed * xdirection );
  ypos = ypos + ( yspeed * ydirection );
  
  // Test to see if the shape exceeds the boundaries of the screen
  // If it does, reverse its direction by multiplying by -1
  if (xpos > width-size || xpos < 0) {
    xdirection *= -1;
  }
  if (ypos  < 0) {
    ydirection *= -1;
  }
  
  //pad mirror
  float pad_x = constrain(mouseX, pad_width, width-pad_width);
  if(speed_bonus_y > height-size-floor_dist  
   && speed_bonus_x >= mouseX
   && speed_bonus_x <= pad_x + pad_width )
   {
     visible_bonus=0;
     pad_width=100;
   }
  if(ypos > height-size-floor_dist  
   && xpos > mouseX
   && xpos < pad_x + pad_width  )
  
  {
    ydirection *= -1;
    yspeed= random(3,5);
  }
  
 if( ypos > height ) 
  {
   
  ypos = 200 ;
  xpos =200 ;
  life -=1;
  if(life<1)//life
 {
   
          javax.swing.JOptionPane.showMessageDialog(null, "GAME OVER");  
          exit();
   //gameover
 } 
  }
  
    visible_count=0;
  //touch box
  float m=20;
  for(int j=0;j<4;j++)
  {
    float n=15;
    for(int i=0;i<10;i++)
    {
      
      if((ypos<=m+5 ||  (ypos<=(m-5)&&ypos>=(m-7)) ) && xpos>=i+n && xpos<=i+n+30 && visible[j][i]==1 )
      {
        
        visible[j][i]=0;
        ydirection *= -1; 
        if(bonus[j][i]>7)
        {
         speed_bonus_val=1;
         speed_bonus_x=xpos;
         speed_bonus_y=ypos;
        }
      }
      n=n+35;
    }
    m=m+20;
  }  

  //draw boxes
   m=20;
for(int j=0; j<4; j++)
{
   int n=15;
   color c1 = color(100+m*3,m+24*n,0);
for(int i=0; i<10;i++)
{
 if(visible[j][i]==1)
{
  fill(c1);
  noStroke();
rect(i+n,m,30,10);
n=n+35;
} else { n=n+35;}
}
m=m+20;
}
  //Draw bonus
  if(speed_bonus_val == 1 )
{
  if(visible_bonus==1)
{
  speed_bonus_y=speed_bonus_y+bonus_speed;
  ellipse( speed_bonus_x, speed_bonus_y, 20, 20);
}
}
  //Draw the paddle 
 rect(mouseX,height- floor_dist,pad_width,pad_height);
  // Draw the shape
  
  ellipse(xpos+size/2, ypos+size/2, size, size);
  
  //draw lifes
  int n=5;

  for(int i=0;i<life;i++)
  {
    fill(#FF0202);

    triangle(40+n, 15, 30+n, 5,50+n, 5);
    n=n+25;
  }}
  
  void keyPressed()
{
  if (keyCode == SHIFT){
      javax.swing.JOptionPane.showMessageDialog(null, "Пауза");      
    }
  

}
